<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Setter Injection</title>
</head>
<body>
<h2>Overview</h2>

<p>Setter Injection is where the container or embedder hands dependencies to a component via setter
    methods after instantiation.</p>

<h2>Example</h2>

<p>Consider a plain class that has one private field and one setter for each dependency that it needs. </p>

<div class="source">
            <pre>
public class Apple {
    private Orange orange;
    private Pear pear;
    private Banana banana;
    public setOrange(Orange orange) {
        this.orange = orange;
    }
    public setPear(Pear pear) {
        this.pear = pear;
    }
    public setBanana(Banana banana) {
        this.banana = banana;
    }
   public void initialize() {
       // all setXXXs are now done :-)
   }
   // other methods

}</pre>
</div>
<p>Some other technologies marked up the need with a doclet tag:</p>
<div class="source">
  <pre>...

  /**
    * @config name=&quot;orange&quot;
    */
    public setOrange(Orange orange) {
        this.orange = orange;
    }

...</pre>
</div>
<h4></h4>
<p>The container use the meta-information to resolve all the dependencies. Components need not be interface/implementation
    separated. That would be the developer's choice.</p>
<h3>Using Setter Injector Components
    Without a Container.</h3>

<p>Setter Injection components can be used directly, without any container. The component-using class will
    continue to compile, but at run time it will be apparent that there are missing dependencies. The downside of
    this is that a developer may miss a setXXX(..) method invocation if they are using the component directly.
    That is fairly small as a risk as it would clearly be caught in the development cycle. Caught in the
    development cycle, but maybe obscurely so with a NullPointerException.</p>

<div class="source">
              <pre>Apple apple = new Apple();
apple.setPear(myPear);</pre>
</div>
<h3>Using Setter Injection with PicoContainer</h3>
<p>The component factory for this is <strong>SetterInjection</strong>.
It only handles setter injection types of components.</p>
<div class="source">
  <pre>pico = new DefaultPicoContainer(new SetterInjection());
pico.addComponent(Apple.class);
pico.addComponent(Banana.class);
pico.addComponent(Pear.class);
pico.addComponent(Orange.class);

// etc
Apple apple = pico.getComponent(Apple.class);</pre>
</div>
<p>Setter methods (those prefixed with 'set') may not be your
  preferred choice. You can force a different prefix to be choosable in
PicoContainer, such as 'init' or 'inject'. If you want to use an prefix other than 'set'...</p>
<div class="source">
  <pre>pico = new DefaultPicoContainer(new SetterInjection(&quot;mySynonymForSet&quot;));
pico.addComponent(Apple.class);
// etc
Apple apple = pico.getComponent(Apple.class)</pre>
</div>
<p>Factory <strong>AdaptiveInjection</strong> can
  also handle setter injection types, though it requires that the
  component was registered with the <a href="properties.html">property</a> 'SDI' in order to activate the Setter Injection functionality. <strong>AdaptiveInjection</strong> will also fall through to constructor injection if there is no SDI
  property. </p>
<h2>References + Comparison</h2>
<p><a href="http://www.martinfowler.com/articles/injection.html#SetterInjectionWithSpring">Setter Injection</a> is a
    <a href="injection.html" title="Dependency Injection">Dependency Injection</a> variant where an object gets all
    dependencies via setter methods.
    PicoContainer support this with
    <a href="http://www.picocontainer.org/picocontainer/latest/picocontainer/apidocs/org/picocontainer/defaults/SetterInjectionComponentAdapter.html">SetterInjection</a>,
    but
    the
    PicoContainer
    team
    recommends
    <a href="constructor-injection.html" title="Constructor Injection">Constructor Injection</a>.</p>

<p>The disadvantage of Setter Injection there is the possibility to forget to set some of the dependencies.</p>
</body>
</html>
